<!-- 公共弹窗 -->
<template>
  <Teleport to="body">
    <div class="notice_dialog_popup">
      <nut-popup style="overflow: visible !important" :close-on-click-overlay="false" v-model:visible="show"
        :z-index="999">
        <div class="notice_icon" v-if="noticeList[currIndex] && show">
          <template v-if="noticeList[currIndex].type == 0">
            <img style="width: 100%; height: 100%" src="/imgs/common/notice_type_0.webp" alt="" />
          </template>
          <template v-if="noticeList[currIndex].type == 1">
            <img style="width: 100%; height: 100%" src="/imgs/common/notice_type_1.webp" alt="" />
          </template>
          <template v-if="noticeList[currIndex].type == 2">
            <img style="width: 100%; height: 100%" src="/imgs/common/notice_type_2.webp" alt="" />
          </template>
        </div>
        <div class="notice_dialog_content">
          <div class="notice_dialog_main" :class="[
            'notice_dialog_' +
            (noticeList[currIndex] ? noticeList[currIndex].type : ''),
          ]">
            <div class="notice_title">{{ noticeTitle }}</div>
            <div class="notice_subtitle" v-if="noticeList[currIndex]">
              <!-- <template v-if="noticeList[currIndex].type == 0">
                <div>Game</div>
                <div>Announcement</div>
              </template>
              <template v-if="noticeList[currIndex].type == 1">
                <div>Maintenance</div>
                <div>Announcement</div>
              </template> -->
              <!-- <template v-if="noticeList[currIndex].type == 2">
                <div>System</div>
                <div>Announcement</div>
              </template> -->
            </div>

            <div class="notice_dialog_body">
              <div class="notice_dialog_swiper">
                <nut-swiper v-if="!loading" style="height: 343px; width: 347px" @change="changSwiper">
                  <nut-swiper-item v-for="(item, i) in noticeList" :key="i" style="height: 343px; width: 347px">
                    <div class="notice_dialog_item">
                      <div class="notice_dialog_texts notice_dialog_title">
                        <div class="notice_dialog_dot"></div>
                        <div class="notice_dialog_dotr">{{ t(item.title) }}</div>
                      </div>
                      <div class="notice_dialog_texts notice_dialog_text">
                        <div class="notice_dialog_dot"></div>
                        <div class="notice_dialog_dotr">{{ t(item.content) }}</div>
                      </div>
                    </div>
                  </nut-swiper-item>
                </nut-swiper>
              </div>

              <!-- <div class="notice_dialog_btn" @click="close">{{ t('home_page_confirm') }}</div> -->

              <div class="notice_dialog_tip" @click="checked = !checked">
                <span style="
                    font-size: 16px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                  ">
                  <iconpark-icon v-if="!checked" name="tancicongxqd01"></iconpark-icon>
                  <iconpark-icon v-if="checked" name="tancicongxqd02"></iconpark-icon>
                </span>
                <span style="margin-left: 5px">{{ t("home_page_notice_donttip") }}</span>
              </div>
            </div>
          </div>

          <div class="dots">
            <div class="dot" :class="{ active_dot: currIndex == i }" :data-id="item.id" v-for="(item, i) in noticeList"
              :key="i"></div>
          </div>

          <div class="not_notice" @click="() => {
            (checked = true), close();
          }
            ">
            {{ t('dontRemind') }}
          </div>

          <div class="close" @click="close">
            <iconpark-icon name="exit"></iconpark-icon>
          </div>
        </div>
      </nut-popup>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import pinia from "@/store/index";
import { storeToRefs } from "pinia";
import { User } from "@/store/user";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const userInfo = User(pinia);
const { noticeList } = storeToRefs(userInfo);

// 弹窗状态
const show = ref(false);
const loading = ref(false);
const open = () => {
  show.value = true;
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 100);
};
const close = () => {
  show.value = false;

  setTimeout(() => {
    if (checked.value) {
      let localIds = [];
      try {
        localIds = JSON.parse(localStorage.getItem("readed_notice_ids") || "[]");
      } catch {
        localIds = [];
      }

      try {
        localIds.push(...noticeList.value.map((item: any) => item.title));

        if (localIds.length > 100) {
          // 最多保留100条id
          localIds = localIds.slice(localIds.length - 100, localIds.length);
        }
      } catch { }

      localStorage.setItem("readed_notice_ids", JSON.stringify(localIds));
    }
  }, 200);
};

// 指示器
const currIndex = ref(0);
const changSwiper = (i: number) => {
  currIndex.value = i;
};

// 不再打开
const checked = ref(false);

// type: 公告类型:0=一般公告，1=维护公告，2=系统公告,
const noticeTitleMap: any = {
  0: t("home_page_notice_title"),
  1: t("home_page_notice_fixtitle"),
  2: t("home_page_notice_systitle"),
};
const noticeTitle = computed(() => {
  let tile = t("home_page_notice_title");
  try {
    tile = noticeTitleMap[noticeList.value[currIndex.value].type];
  } catch {
    tile = t("home_page_notice_title");
  }
  return tile;
});

defineExpose({
  open,
  close,
});
</script>

<style lang="scss">
.notice_dialog_popup {
  user-select: none;

  .nut-popup {
    background-color: rgba(0, 0, 0, 0);
  }

  .notice_icon {
    width: 160px;
    height: 160px;
    z-index: 1001;
    position: fixed;
    right: 18px;
    top: -40px;
  }
}

.notice_dialog_content {
  div {
    box-sizing: border-box;
    user-select: none;
  }

  .notice_dialog_main {
    position: relative;
    border-radius: 16px;
    background-image: url("/imgs/common/notice_bg.webp");
    background-repeat: no-repeat;
    background-size: 100% auto;
    width: 347px;
    height: 437px;
    padding: 94px 0 0 0;
    border-radius: 12px;
    overflow: hidden;

    .notice_title {
      color: #fff;
      font-size: 24px;
      font-weight: 900;
      line-height: 24px;
      color: #fff;
      position: absolute;
      top: 29px;
      left: 22px;
      width: 150px;
    }

    .notice_subtitle {
      color: #fff;
      font-size: 14px;
      font-weight: 500;
      line-height: 16px;
      position: absolute;
      top: 50px;
      left: 22px;
    }

    .notice_dialog_body {}

    .notice_dialog_swiper {
      height: 343px;
      width: 100%;
      border-radius: 16px;
      background: #131232;
      box-shadow: 0.5px 0.5px 1px 0px #2e296b inset;
    }

    .notice_dialog_btn {
      width: 208px;
      height: 38px;
      flex-grow: 0;
      border-radius: 48px;
      background-color: #5811cb;
      margin: 0 auto;
      font-size: 15px;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .notice_dialog_tip {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      color: #a5a5b6;
      margin-top: 13px;
    }

    .notice_dialog_item {
      padding: 32px 30px 64px 30px;
      font-size: 12px;
      height: 100%;
      overflow-y: auto;

      .notice_dialog_title {
        margin-bottom: 10px;
        color: #c6c4f5;
        font-size: 14px;
        font-weight: 500;
      }

      .notice_dialog_text {
        color: #c6c4f5;
        font-size: 14px;
        font-weight: 500;
      }

      .notice_dialog_texts {
        display: flex;
        align-items: flex-start;

        .notice_dialog_dot {
          width: 4px;
          height: 4px;
          background-color: #7845fe;
          border-radius: 50%;
          margin-right: 6px;
          position: relative;
          top: 7px;
        }

        .notice_dialog_dotr {
          flex: 1;
          word-break: break-all;
        }
      }
    }
  }

  .notice_dialog_1 {
    // background-image: url('/imgs/home/notice_bg_1.webp');
  }

  .notice_dialog_2 {
    // background-image: url('/imgs/home/notice_bg_2.webp');
  }

  .dots {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 7px;
    position: relative;
    top: -64px;

    .dot {
      width: 8px;
      height: 8px;
      background-color: #c6c4f5;
      opacity: 0.4;
      border-radius: 4px;
      margin: 0 3px;
      transition: all 0.2s ease;
    }

    .active_dot {
      opacity: 1;
    }
  }

  .not_notice {
    color: #504f6f;
    text-align: center;
    font-family: "PingFang SC";
    font-size: 12px;
    font-style: normal;
    font-weight: 500;
    line-height: 12px;
    /* 100% */
    text-decoration-line: underline;
    text-decoration-style: solid;
    text-decoration-skip-ink: auto;
    text-decoration-thickness: auto;
    text-underline-offset: auto;
    position: relative;
    top: -46px;
    cursor: pointer;
  }

  .close {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: solid 2px #fff;
    background-color: rgba(217, 217, 217, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    margin: 0 auto;
    position: relative;
  }
}
</style>
